package com.xtianzhuang.www.controller.pages;

import java.text.SimpleDateFormat;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.TfLoginLog;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.LoginLogResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/pages")
public class PageController extends BaseController {
	/**
	  * 生活
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index")
	@RequiresPermissions("life")
	public ModelAndView index(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("index");
		// 上次登录日志
		TfLoginLog tfLoginLog = userServiceBiz.getLastloginInfo(subject.getPrincipal().getUserId());
		if (tfLoginLog != null) {
			LoginLogResponse log = new LoginLogResponse();
			log.setLoginIp(tfLoginLog.getLoginIp());
			SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			log.setLoginTime(sdFormat.format(tfLoginLog.getLoginTime()));
			model.addObject("log", log);
		}
		// 生日提醒
		try {
			if (subject.getPrincipal().getUserType().equals("TS00")) {
				String remind = userServiceBiz.selectBirthDayRemindUser();
				model.addObject("remind", remind);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}

	@RequestMapping("/getIndex")
	public ModelAndView getIndex(BaseRequest request, BaseResponse response) {
		ModelAndView model = null;
		if (subject.getPrincipal().getUserType().equals("TD01")) {
			model = new ModelAndView("index3");
		} else {
			model = new ModelAndView("index");
			// 上次登录日志
			TfLoginLog tfLoginLog = userServiceBiz.getLastloginInfo(subject.getPrincipal().getUserId());
			if (tfLoginLog != null) {
				LoginLogResponse log = new LoginLogResponse();
				log.setLoginIp(tfLoginLog.getLoginIp());
				SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				log.setLoginTime(sdFormat.format(tfLoginLog.getLoginTime()));
				model.addObject("log", log);
			}
			// 生日提醒
			try {
				if (subject.getPrincipal().getUserType().equals("TS00")) {
					String remind = userServiceBiz.selectBirthDayRemindUser();
					model.addObject("remind", remind);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}


	/**
	  * 财务
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index2")
	@RequiresPermissions("finance")
	public ModelAndView index2(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("index2");
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}

	/**
	  * 项目
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index3")
	public ModelAndView index3(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("index3");
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}

	/**
	  * 系统
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index4")
	@RequiresPermissions("project")
	public ModelAndView index4(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("index4");
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}

	/**
	  * 学习
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/index5")
	@RequiresPermissions("study")
	public ModelAndView index5(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("index5");
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}

	@RequestMapping("/noPermission")
	public ModelAndView noPermission(BaseRequest request, BaseResponse response) {
		ModelAndView model = new ModelAndView("noPermission");
		super.executeBase(request, response);
		model.addObject("res", response);
		return model;
	}
}
